<template>
	<view>
		<view class="card">

			<view v-if="!isAd">
				<view class="smart-flex smart-row">
					<view v-if="!isTop||mode == 2">
						<image :src="showImg" style="width: 80px; height: 100px;" mode="widthFix"
							@error="handleImageException(0)"></image>
					</view>
					<text class="titlebox">{{title}}</text>
				</view>


				<view class="tipbox">
					<text v-if="isTop" style="color: red;margin-left: 8px;">置顶</text>
					<text>{{author}}</text>
					<text>{{comment}}评</text>

					<view style="display: flex; flex: 1; justify-content: flex-end;">
						<text>{{timedata}}</text>
					</view>

					<slot name="tips"></slot>
				</view>
			</view>

			<view v-if="isAd">
				<view>
					<text style="font-size: 16px; color: #777;">{{title}}</text>
				</view>

				<view class="smart-flex smart-row" style="justify-content: space-around;">
					<view v-for="(item,index) in images">
						<image :src="item" style="width: 100px; height: 100px; margin: 5px; border-radius: 8px;" mode="aspectFill;" @error="handleImageException(index)"></image>
					</view>
				</view>
				<view class="tipbox">
					<text style="color: blue;">广告</text>
					<text>{{author}}</text>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		name: "TopTextNews",
		data() {
			return {
				defaultImg: "/static/img/defluatImage.png",
				failImg: "/static/img/failImage.png",
				imageError: false
			}
		},

		computed: {
			showImg() {
				if (this.images[0]) {
					if (this.imageError) {
						console.log("showImage Error-->" + this.title);
					}
					return this.imageError ? this.failImg : this.images[0]
				} else {
					console.log("showImage Default-->" + this.title);
					return this.defaultImg
				}
			}
		},

		props: {
			title: {
				type: String,
				default: "新闻标题",
				require: true
			},
			isTop: {
				type: Boolean,
				default: true,
				require: true
			},
			isAd: {
				type: Boolean,
				default: false,
				require: true
			},
			author: {
				type: String,
				default: "作者",
				require: true
			},
			comment: {
				type: String,
				default: "0",
				require: true
			},
			timedata: {
				type: String,
				default: "1999.12.31",
				require: true
			},
			mode: {
				type: Number,
				default: 1,
				require: true
			},
			images: {
				type: Array,
				default: () => ["/static/img/defluatImage.png"]
			}

		},
		methods: {
			handleImageException(index) {
				console.log("handleImageException-->index:" + this.title);
				this.$set(this.images, index, this.failImg);
				this.imageError = false;
			}
		}
	}
</script>

<style>
	.card {
		border-radius: 8px;
		padding: 10px;
		border: 1px solid #DDD;
	}

	.titlebox {
		font-size: 20px;
		margin-top: 5px;
		margin-left: 8px;
	}

	.tipbox {
		display: flex;
		margin-top: 5px;
		font-size: 14px;
		color: #aaa;
	}

	.tipbox>text {
		margin-right: 10px;
	}
</style>